package com.itheima.mobilesafe69.activity;

import com.itheima.mobilesafe69.R;
import com.itheima.mobilesafe69.utils.ConstantValue;
import com.itheima.mobilesafe69.utils.PrefUtils;
import com.itheima.mobilesafe69.utils.ToastUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class Setup4Activity extends BaseSetupActivity {
	private CheckBox cb_box_open_protect;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setup4);
		
		initUI();
	}
	
	private void initUI() {
		cb_box_open_protect = (CheckBox) findViewById(R.id.cb_box_open_protect);
		//sp中获取其初始状态(选中和未选中)
		boolean is_open_protect = PrefUtils.getBoolean(this,ConstantValue.OPEN_PROTECT, false);
		cb_box_open_protect.setChecked(is_open_protect);
		
		if(is_open_protect){
			cb_box_open_protect.setText("防盗保护已开启");
		}else{
			cb_box_open_protect.setText("防盗保护已关闭");
		}
		
		//在点击此单选框是,选中状态的切换(选中--->未选中,未选中---->选中),是否选中的事件
		cb_box_open_protect.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				//isChecked代表点击checkbox以后的一个选中状态
				if(isChecked){
					cb_box_open_protect.setText("防盗保护已开启");
				}else{
					cb_box_open_protect.setText("防盗保护已关闭");
				}
				//本地存储选中状态
				PrefUtils.putBoolean(Setup4Activity.this,ConstantValue.OPEN_PROTECT, isChecked);
			}
		});
	}
	@Override
	public void showNextPage() {
		boolean is_open_protect = PrefUtils.getBoolean(this,ConstantValue.OPEN_PROTECT,false);
		if(is_open_protect){
			//如果能够跳转到下一页,则需要将设置完成的标志将其设置为true
			PrefUtils.putBoolean(this,ConstantValue.SETUP_OVER, true);
			startActivity(new Intent(this, MobileSafeActivity.class));
			finish();
		}else{
			ToastUtil.show(this,"必须开启防盗保护");
		}
		overridePendingTransition(R.anim.next_in_translate, R.anim.next_out_translate);
	}

	@Override
	public void showPrePage() {
		startActivity(new Intent(this, Setup3Activity.class));
		finish();
		
		//上一页移入一出动画
		overridePendingTransition(R.anim.pre_in_translate, R.anim.pre_out_translate);
	}
}
